/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuscarPartidoElectoral.java
 *
 * Created on 14-ene-2013, 6:43:32
 */
package sisvoto.vista;

import java.util.ArrayList;
import sisvoto.DAO.ConsultasBD;
import sisvoto.modelo.Eleccion;
import sisvoto.modeloTablas.ModelTableElecciones;

/**
 *
 * @author DJ2-PC
 */
public class BuscarElecciones extends javax.swing.JDialog {

    ModelTableElecciones modelo;
    Eleccion eleccion;
    ArrayList<Eleccion> elecciones;
    AdministrarElecciones admelec = null;
    Result resultados = null;

    /** Creates new form BuscarPartidoElectoral */
    
    public BuscarElecciones(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        actualizartabla((ArrayList<Eleccion>) new ConsultasBD().buscarTodos("Eleccion"));
    }
    
    public BuscarElecciones(java.awt.Frame parent, boolean modal, AdministrarElecciones admelec) {
        super(parent, modal);
        this.admelec = admelec;
        initComponents();
        actualizartabla((ArrayList<Eleccion>) new ConsultasBD().buscarTodos("Eleccion"));
    }

    public BuscarElecciones(java.awt.Frame parent, boolean modal, Result resultados) {
        super(parent, modal);
        this.resultados = resultados;
        initComponents();
        actualizartabla((ArrayList<Eleccion>) new ConsultasBD().buscarTodos("Eleccion"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblBusPartElec = new javax.swing.JLabel();
        jLblCrtBus = new javax.swing.JLabel();
        jCbxelecciones = new javax.swing.JComboBox();
        txtBuscar = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTblPartidoElectoral = new javax.swing.JTable();
        jBtnImportar = new javax.swing.JButton();
        jBtnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Partidos Electorales");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N

        curvesPanel1.setLayout(null);

        jLblBusPartElec.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblBusPartElec.setForeground(new java.awt.Color(255, 255, 255));
        jLblBusPartElec.setText("Elecciones");
        curvesPanel1.add(jLblBusPartElec);
        jLblBusPartElec.setBounds(170, 30, 250, 21);

        jLblCrtBus.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblCrtBus.setForeground(new java.awt.Color(255, 255, 255));
        jLblCrtBus.setText("Criterio de Búsqueda:");
        curvesPanel1.add(jLblCrtBus);
        jLblCrtBus.setBounds(30, 70, 160, 30);

        jCbxelecciones.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Titulo", "Estado", "Todos" }));
        curvesPanel1.add(jCbxelecciones);
        jCbxelecciones.setBounds(200, 70, 230, 30);
        curvesPanel1.add(txtBuscar);
        txtBuscar.setBounds(30, 110, 270, 40);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/search_32.png"))); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        curvesPanel1.add(jButton1);
        jButton1.setBounds(311, 110, 120, 40);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Elecciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), java.awt.Color.white)); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(null);

        jTblPartidoElectoral.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTblPartidoElectoral.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTblPartidoElectoralMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTblPartidoElectoral);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 20, 380, 130);

        curvesPanel1.add(jPanel1);
        jPanel1.setBounds(30, 160, 400, 160);

        jBtnImportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/right_32.png"))); // NOI18N
        jBtnImportar.setText("Importar");
        jBtnImportar.setEnabled(false);
        jBtnImportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnImportarActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnImportar);
        jBtnImportar.setBounds(130, 330, 110, 41);

        jBtnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/DeleteRed.png"))); // NOI18N
        jBtnSalir.setText("Salir");
        jBtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSalirActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnSalir);
        jBtnSalir.setBounds(250, 330, 110, 41);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    jBtnImportar.setEnabled(false);
    if (jCbxelecciones.getSelectedItem().toString().equals("Titulo")) {
        this.elecciones = (ArrayList<Eleccion>) new ConsultasBD().buscarLIKE("Eleccion", "titulo", txtBuscar.getText());
        actualizartabla(elecciones);
    } else {
        if (jCbxelecciones.getSelectedItem().toString().equals("Todos")) {
            this.elecciones = (ArrayList<Eleccion>) new ConsultasBD().buscarTodos("Eleccion");
        } else {
            this.elecciones = (ArrayList<Eleccion>) new ConsultasBD().buscarLIKE("Eleccion", "estado", txtBuscar.getText());
        }
        actualizartabla(elecciones);
    }

}//GEN-LAST:event_jButton1ActionPerformed

private void jTblPartidoElectoralMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTblPartidoElectoralMouseClicked
// TODO add your handling code here:
    if (jTblPartidoElectoral.getSelectedRow() != -1) {
        Integer idpriv = (Integer) jTblPartidoElectoral.getValueAt(jTblPartidoElectoral.getSelectedRow(), 0);
        ArrayList<Eleccion> paux = (ArrayList<Eleccion>) new ConsultasBD().buscarPOR("Eleccion", "id", "'" + idpriv + "'");
        this.eleccion = paux.get(0);
        jBtnImportar.setEnabled(true);
    }
}//GEN-LAST:event_jTblPartidoElectoralMouseClicked

private void jBtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSalirActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jBtnSalirActionPerformed

private void jBtnImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnImportarActionPerformed
// TODO add your handling code here:
    if (admelec != null) {
        AdministrarElecciones ap;
        this.dispose();
        try {
            admelec.dispose();
            ap = new AdministrarElecciones(null, true, eleccion);
            ap.setLocationRelativeTo(this);
            ap.setVisible(true);
        } catch (Exception e) {
        }
    }
    if(resultados!=null){
        Result res;
        this.dispose();
        try {
            resultados.dispose();
            res = new Result(eleccion);
            res.setLocationRelativeTo(this);
            res.setVisible(true);
        } catch (Exception e) {
        }
    }
        



}//GEN-LAST:event_jBtnImportarActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JButton jBtnImportar;
    private javax.swing.JButton jBtnSalir;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCbxelecciones;
    private javax.swing.JLabel jLblBusPartElec;
    private javax.swing.JLabel jLblCrtBus;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTblPartidoElectoral;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables

    private void actualizartabla(ArrayList<Eleccion> elecciones) {
        this.elecciones = elecciones;
        modelo = new ModelTableElecciones(elecciones);
        jTblPartidoElectoral.setModel(modelo);
        jTblPartidoElectoral.updateUI();
    }
}
